package com.free.utils.crypt;

import com.free.utils.CharsetUtils;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;


public class SHACrypt
{
    public static byte[] sha256(String text)
    {
        return sha256(text, CharsetUtils.UTF8_CHARSET);
    }

    public static byte[] sha256(String text, Charset charset)
    {
        return DigestUtils.sha256(text.getBytes(charset));
    }

    public static String sha256Hex(String text)
    {
        return sha256Hex(text, CharsetUtils.UTF8_CHARSET);
    }

    public static String sha256Hex(String text, Charset charset)
    {
        return DigestUtils.sha256Hex(text.getBytes(charset));
    }

    public static String sha1Hex(String text)
    {
        return sha1Hex(text, CharsetUtils.UTF8_CHARSET);
    }

    public static String sha1Hex(String text, Charset charset)
    {
        return DigestUtils.sha1Hex(text.getBytes(charset));
    }

    // TEST
    public static void main(String[] args) throws UnsupportedEncodingException
    {
        String str = "186519836881gxjz8gpG";

        System.out.println(Hex.encodeHex(sha256(str, Charset.forName(CharsetUtils.UTF8))));
    }
}
